#include <iostream>
#include <unordered_map>

using namespace std;
// using int64_t long long;
typedef long long LL;
LL d, p, q;

LL gcd(LL a, LL b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    cin >> d >> p >> q;
    LL g = gcd(p, d);
    LL t = p / g;
    
    while(q--)
    {   
        LL l, r; cin >> l >> r;
        if(p == 1)
        {
            int ans = 1;
            if(l == 1 && r > l) ans++;
            cout << ans << endl;
            continue;
        }
        cout << min(t, r - l + 1) << endl;
    }
    return 0;
}
